home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_xemacs.idb / usr / freeware / lib / xemacs-20.4 / lisp / gnus / gnus-cache.el.z / gnus-cache.el
Encoding:
Text File  |  1998-05-21  |  22.4 KB  |  665 lines

  1. ;;; gnus-cache.el --- cache interface for Gnus
  2. ;; Copyright (C) 1995,96,97 Free Software Foundation, Inc.
  3.  
  4. ;; Author: Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
  5. ;; Keywords: news
  6.  
  7. ;; This file is part of GNU Emacs.
  8.  
  9. ;; GNU Emacs is free software; you can redistribute it and/or modify
  10. ;; it under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation; either version 2, or (at your option)
  12. ;; any later version.
  13.  
  14. ;; GNU Emacs is distributed in the hope that it will be useful,
  15. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17. ;; GNU General Public License for more details.
  18.  
  19. ;; You should have received a copy of the GNU General Public License
  20. ;; along with GNU Emacs; see the file COPYING.  If not, write to the
  21. ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  22. ;; Boston, MA 02111-1307, USA.
  23.  
  24. ;;; Commentary:
  25.  
  26. ;;; Code:
  27.  
  28. (eval-when-compile (require 'cl))
  29.  
  30. (require 'gnus)
  31. (require 'gnus-int)
  32. (require 'gnus-range)
  33. (require 'gnus-start)
  34. (eval-when-compile
  35.   (require 'gnus-sum))
  36.  
  37. (defgroup gnus-cache nil
  38.   "Cache interface."
  39.   :group 'gnus)
  40.  
  41. (defcustom gnus-cache-directory
  42.   (nnheader-concat gnus-directory "cache/")
  43.   "*The directory where cached articles will be stored."
  44.   :group 'gnus-cache
  45.   :type 'directory)
  46.  
  47. (defcustom gnus-cache-active-file
  48.   (concat (file-name-as-directory gnus-cache-directory) "active")
  49.   "*The cache active file."
  50.   :group 'gnus-cache
  51.   :type 'file)
  52.  
  53. (defcustom gnus-cache-enter-articles '(ticked dormant)
  54.   "Classes of articles to enter into the cache."
  55.   :group 'gnus-cache
  56.   :type '(set (const ticked) (const dormant) (const unread) (const read)))
  57.  
  58. (defcustom gnus-cache-remove-articles '(read)
  59.   "Classes of articles to remove from the cache."
  60.   :group 'gnus-cache
  61.   :type '(set (const ticked) (const dormant) (const unread) (const read)))
  62.  
  63. (defcustom gnus-uncacheable-groups nil
  64.   "*Groups that match this regexp will not be cached.
  65.  
  66. If you want to avoid caching your nnml groups, you could set this
  67. variable to \"^nnml\"."
  68.   :group 'gnus-cache
  69.   :type '(choice (const :tag "off" nil)
  70.          regexp))
  71.  
  72.  
  73.  
  74. ;;; Internal variables.
  75.  
  76. (defvar gnus-cache-removable-articles nil)
  77. (defvar gnus-cache-buffer nil)
  78. (defvar gnus-cache-active-hashtb nil)
  79. (defvar gnus-cache-active-altered nil)
  80.  
  81. (eval-and-compile
  82.   (autoload 'nnml-generate-nov-databases-1 "nnml")
  83.   (autoload 'nnvirtual-find-group-art "nnvirtual"))
  84.  
  85.  
  86.  
  87. ;;; Functions called from Gnus.
  88.  
  89. (defun gnus-cache-open ()
  90.   "Initialize the cache."
  91.   (when (or (file-exists-p gnus-cache-directory)
  92.         (and gnus-use-cache
  93.          (not (eq gnus-use-cache 'passive))))
  94.     (gnus-cache-read-active)))
  95.  
  96. ;; Complexities of byte-compiling make this kludge necessary.  Eeek.
  97. (ignore-errors
  98.   (gnus-add-shutdown 'gnus-cache-close 'gnus))
  99.  
  100. (defun gnus-cache-close ()
  101.   "Shut down the cache."
  102.   (gnus-cache-write-active)
  103.   (gnus-cache-save-buffers)
  104.   (setq gnus-cache-active-hashtb nil))
  105.  
  106. (defun gnus-cache-save-buffers ()
  107.   ;; save the overview buffer if it exists and has been modified
  108.   ;; delete empty cache subdirectories
  109.   (when gnus-cache-buffer
  110.     (let ((buffer (cdr gnus-cache-buffer))
  111.       (overview-file (gnus-cache-file-name
  112.               (car gnus-cache-buffer) ".overview")))
  113.       ;; write the overview only if it was modified
  114.       (when (buffer-modified-p buffer)
  115.     (save-excursion
  116.       (set-buffer buffer)
  117.       (if (> (buffer-size) 0)
  118.           ;; Non-empty overview, write it to a file.
  119.           (gnus-write-buffer overview-file)
  120.         ;; Empty overview file, remove it
  121.         (when (file-exists-p overview-file)
  122.           (delete-file overview-file))
  123.         ;; If possible, remove group's cache subdirectory.
  124.         (condition-case nil
  125.         ;; FIXME: we can detect the error type and warn the user
  126.         ;; of any inconsistencies (articles w/o nov entries?).
  127.         ;; for now, just be conservative...delete only if safe -- sj
  128.         (delete-directory (file-name-directory overview-file))
  129.           (error nil)))))
  130.       ;; Kill the buffer -- it's either unmodified or saved.
  131.       (gnus-kill-buffer buffer)
  132.       (setq gnus-cache-buffer nil))))
  133.  
  134. (defun gnus-cache-possibly-enter-article
  135.   (group article headers ticked dormant unread &optional force)
  136.   (when (and (or force (not (eq gnus-use-cache 'passive)))
  137.          (numberp article)
  138.          (> article 0)
  139.          (vectorp headers))        ; This might be a dummy article.
  140.     ;; If this is a virtual group, we find the real group.
  141.     (when (gnus-virtual-group-p group)
  142.       (let ((result (nnvirtual-find-group-art
  143.              (gnus-group-real-name group) article)))
  144.     (setq group (car result)
  145.           headers (copy-sequence headers))
  146.     (mail-header-set-number headers (cdr result))))
  147.     (let ((number (mail-header-number headers))
  148.       file dir)
  149.       (when (and number
  150.          (> number 0)        ; Reffed article.
  151.          (or force
  152.              (and (or (not gnus-uncacheable-groups)
  153.                   (not (string-match
  154.                     gnus-uncacheable-groups group)))
  155.               (gnus-cache-member-of-class
  156.                gnus-cache-enter-articles ticked dormant unread)))
  157.          (not (file-exists-p (setq file (gnus-cache-file-name
  158.                          group number)))))
  159.     ;; Possibly create the cache directory.
  160.     (gnus-make-directory (setq dir (file-name-directory file)))
  161.     ;; Save the article in the cache.
  162.     (if (file-exists-p file)
  163.         t                ; The article already is saved.
  164.       (save-excursion
  165.         (set-buffer nntp-server-buffer)
  166.         (let ((gnus-use-cache nil))
  167.           (gnus-request-article-this-buffer number group))
  168.         (when (> (buffer-size) 0)
  169.           (gnus-write-buffer file)
  170.           (gnus-cache-change-buffer group)
  171.           (set-buffer (cdr gnus-cache-buffer))
  172.           (goto-char (point-max))
  173.           (forward-line -1)
  174.           (while (condition-case ()
  175.              (when (not (bobp))
  176.                (> (read (current-buffer)) number))
  177.                (error
  178.             ;; The line was malformed, so we just remove it!!
  179.             (gnus-delete-line)
  180.             t))
  181.         (forward-line -1))
  182.           (if (bobp)
  183.           (if (not (eobp))
  184.               (progn
  185.             (beginning-of-line)
  186.             (when (< (read (current-buffer)) number)
  187.               (forward-line 1)))
  188.             (beginning-of-line))
  189.         (forward-line 1))
  190.           (beginning-of-line)
  191.           ;; [number subject from date id references chars lines xref]
  192.           (insert (format "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t\n"
  193.                   (mail-header-number headers)
  194.                   (mail-header-subject headers)
  195.                   (mail-header-from headers)
  196.                   (mail-header-date headers)
  197.                   (mail-header-id headers)
  198.                   (or (mail-header-references headers) "")
  199.                   (or (mail-header-chars headers) "")
  200.                   (or (mail-header-lines headers) "")
  201.                   (or (mail-header-xref headers) "")))
  202.           ;; Update the active info.
  203.           (set-buffer gnus-summary-buffer)
  204.           (gnus-cache-update-active group number)
  205.           (push article gnus-newsgroup-cached)
  206.           (gnus-summary-update-secondary-mark article))
  207.         t))))))
  208.  
  209. (defun gnus-cache-enter-remove-article (article)
  210.   "Mark ARTICLE for later possible removal."
  211.   (when article
  212.     (push article gnus-cache-removable-articles)))
  213.  
  214. (defun gnus-cache-possibly-remove-articles ()
  215.   "Possibly remove some of the removable articles."
  216.   (if (not (gnus-virtual-group-p gnus-newsgroup-name))
  217.       (gnus-cache-possibly-remove-articles-1)
  218.     (let ((arts gnus-cache-removable-articles)
  219.       ga)
  220.       (while arts
  221.     (when (setq ga (nnvirtual-find-group-art
  222.             (gnus-group-real-name gnus-newsgroup-name) (pop arts)))
  223.       (let ((gnus-cache-removable-articles (list (cdr ga)))
  224.         (gnus-newsgroup-name (car ga)))
  225.         (gnus-cache-possibly-remove-articles-1)))))
  226.     (setq gnus-cache-removable-articles nil)))
  227.  
  228. (defun gnus-cache-possibly-remove-articles-1 ()
  229.   "Possibly remove some of the removable articles."
  230.   (unless (eq gnus-use-cache 'passive)
  231.     (let ((articles gnus-cache-removable-articles)
  232.       (cache-articles gnus-newsgroup-cached)
  233.       article)
  234.       (gnus-cache-change-buffer gnus-newsgroup-name)
  235.       (while articles
  236.     (when (memq (setq article (pop articles)) cache-articles)
  237.       ;; The article was in the cache, so we see whether we are
  238.       ;; supposed to remove it from the cache.
  239.       (gnus-cache-possibly-remove-article
  240.        article (memq article gnus-newsgroup-marked)
  241.        (memq article gnus-newsgroup-dormant)
  242.        (or (memq article gnus-newsgroup-unreads)
  243.            (memq article gnus-newsgroup-unselected))))))
  244.     ;; The overview file might have been modified, save it
  245.     ;; safe because we're only called at group exit anyway.
  246.     (gnus-cache-save-buffers)))
  247.  
  248. (defun gnus-cache-request-article (article group)
  249.   "Retrieve ARTICLE in GROUP from the cache."
  250.   (let ((file (gnus-cache-file-name group article))
  251.     (buffer-read-only nil))
  252.     (when (file-exists-p file)
  253.       (erase-buffer)
  254.       (gnus-kill-all-overlays)
  255.       (insert-file-contents file)
  256.       t)))
  257.  
  258. (defun gnus-cache-possibly-alter-active (group active)
  259.   "Alter the ACTIVE info for GROUP to reflect the articles in the cache."
  260.   (when gnus-cache-active-hashtb
  261.     (let ((cache-active (gnus-gethash group gnus-cache-active-hashtb)))
  262.       (when cache-active
  263.     (when (< (car cache-active) (car active))
  264.       (setcar active (car cache-active)))
  265.     (when (> (cdr cache-active) (cdr active))
  266.       (setcdr active (cdr cache-active)))))))
  267.  
  268. (defun gnus-cache-retrieve-headers (articles group &optional fetch-old)
  269.   "Retrieve the headers for ARTICLES in GROUP."
  270.   (let ((cached
  271.      (setq gnus-newsgroup-cached (gnus-cache-articles-in-group group))))
  272.     (if (not cached)
  273.     ;; No cached articles here, so we just retrieve them
  274.     ;; the normal way.
  275.     (let ((gnus-use-cache nil))
  276.       (gnus-retrieve-headers articles group fetch-old))
  277.       (let ((uncached-articles (gnus-sorted-intersection
  278.                 (gnus-sorted-complement articles cached)
  279.                 articles))
  280.         (cache-file (gnus-cache-file-name group ".overview"))
  281.         type)
  282.     ;; We first retrieve all the headers that we don't have in
  283.     ;; the cache.
  284.     (let ((gnus-use-cache nil))
  285.       (when uncached-articles
  286.         (setq type (and articles
  287.                 (gnus-retrieve-headers
  288.                  uncached-articles group fetch-old)))))
  289.     (gnus-cache-save-buffers)
  290.     ;; Then we insert the cached headers.
  291.     (save-excursion
  292.       (cond
  293.        ((not (file-exists-p cache-file))
  294.         ;; There are no cached headers.
  295.         type)
  296.        ((null type)
  297.         ;; There were no uncached headers (or retrieval was
  298.         ;; unsuccessful), so we use the cached headers exclusively.
  299.         (set-buffer nntp-server-buffer)
  300.         (erase-buffer)
  301.         (insert-file-contents cache-file)
  302.         'nov)
  303.        ((eq type 'nov)
  304.         ;; We have both cached and uncached NOV headers, so we
  305.         ;; braid them.
  306.         (gnus-cache-braid-nov group cached)
  307.         type)
  308.        (t
  309.         ;; We braid HEADs.
  310.         (gnus-cache-braid-heads group (gnus-sorted-intersection
  311.                        cached articles))
  312.         type)))))))
  313.  
  314. (defun gnus-cache-enter-article (&optional n)
  315.   "Enter the next N articles into the cache.
  316. If not given a prefix, use the process marked articles instead.
  317. Returns the list of articles entered."
  318.   (interactive "P")
  319.   (gnus-set-global-variables)
  320.   (let ((articles (gnus-summary-work-articles n))
  321.     article out)
  322.     (while (setq article (pop articles))
  323.       (if (natnump article)
  324.       (when (gnus-cache-possibly-enter-article
  325.          gnus-newsgroup-name article
  326.          (gnus-summary-article-header article)
  327.          nil nil nil t)
  328.         (push article out))
  329.     (gnus-message 2 "Can't cache article %d" article))
  330.       (gnus-summary-remove-process-mark article)
  331.       (gnus-summary-update-secondary-mark article))
  332.     (gnus-summary-next-subject 1)
  333.     (gnus-summary-position-point)
  334.     (nreverse out)))
  335.  
  336. (defun gnus-cache-remove-article (n)
  337.   "Remove the next N articles from the cache.
  338. If not given a prefix, use the process marked articles instead.
  339. Returns the list of articles removed."
  340.   (interactive "P")
  341.   (gnus-set-global-variables)
  342.   (gnus-cache-change-buffer gnus-newsgroup-name)
  343.   (let ((articles (gnus-summary-work-articles n))
  344.     article out)
  345.     (while articles
  346.       (setq article (pop articles))
  347.       (when (gnus-cache-possibly-remove-article article nil nil nil t)
  348.     (push article out))
  349.       (gnus-summary-remove-process-mark article)
  350.       (gnus-summary-update-secondary-mark article))
  351.     (gnus-summary-next-subject 1)
  352.     (gnus-summary-position-point)
  353.     (nreverse out)))
  354.  
  355. (defun gnus-cached-article-p (article)
  356.   "Say whether ARTICLE is cached in the current group."
  357.   (memq article gnus-newsgroup-cached))
  358.  
  359. (defun gnus-summary-insert-cached-articles ()
  360.   "Insert all the articles cached for this group into the current buffer."
  361.   (interactive)
  362.   (let ((cached gnus-newsgroup-cached)
  363.     (gnus-verbose (max 6 gnus-verbose)))
  364.     (unless cached
  365.       (error "No cached articles for this group"))
  366.     (while cached
  367.       (gnus-summary-goto-subject (pop cached) t))))
  368.  
  369. ;;; Internal functions.
  370.  
  371. (defun gnus-cache-change-buffer (group)
  372.   (and gnus-cache-buffer
  373.        ;; See if the current group's overview cache has been loaded.
  374.        (or (string= group (car gnus-cache-buffer))
  375.        ;; Another overview cache is current, save it.
  376.        (gnus-cache-save-buffers)))
  377.   ;; if gnus-cache buffer is nil, create it
  378.   (unless gnus-cache-buffer
  379.     ;; Create cache buffer
  380.     (save-excursion
  381.       (setq gnus-cache-buffer
  382.         (cons group
  383.           (set-buffer (get-buffer-create " *gnus-cache-overview*"))))
  384.       (buffer-disable-undo (current-buffer))
  385.       ;; Insert the contents of this group's cache overview.
  386.       (erase-buffer)
  387.       (let ((file (gnus-cache-file-name group ".overview")))
  388.     (when (file-exists-p file)
  389.       (nnheader-insert-file-contents file)))
  390.       ;; We have a fresh (empty/just loaded) buffer,
  391.       ;; mark it as unmodified to save a redundant write later.
  392.       (set-buffer-modified-p nil))))
  393.  
  394. ;; Return whether an article is a member of a class.
  395. (defun gnus-cache-member-of-class (class ticked dormant unread)
  396.   (or (and ticked (memq 'ticked class))
  397.       (and dormant (memq 'dormant class))
  398.       (and unread (memq 'unread class))
  399.       (and (not unread) (not ticked) (not dormant) (memq 'read class))))
  400.  
  401. (defun gnus-cache-file-name (group article)
  402.   (concat (file-name-as-directory gnus-cache-directory)
  403.       (file-name-as-directory
  404.        (nnheader-translate-file-chars
  405.         (if (gnus-use-long-file-name 'not-cache)
  406.         group
  407.           (let ((group (nnheader-replace-chars-in-string group ?/ ?_)))
  408.         ;; Translate the first colon into a slash.
  409.         (when (string-match ":" group)
  410.           (aset group (match-beginning 0) ?/))
  411.         (nnheader-replace-chars-in-string group ?. ?/)))))
  412.       (if (stringp article) article (int-to-string article))))
  413.  
  414. (defun gnus-cache-update-article (group article)
  415.   "If ARTICLE is in the cache, remove it and re-enter it."
  416.   (when (gnus-cache-possibly-remove-article article nil nil nil t)
  417.     (let ((gnus-use-cache nil))
  418.       (gnus-cache-possibly-enter-article
  419.        gnus-newsgroup-name article (gnus-summary-article-header article)
  420.        nil nil nil t))))
  421.  
  422. (defun gnus-cache-possibly-remove-article (article ticked dormant unread
  423.                            &optional force)
  424.   "Possibly remove ARTICLE from the cache."
  425.   (let ((group gnus-newsgroup-name)
  426.     (number article)
  427.     file)
  428.     ;; If this is a virtual group, we find the real group.
  429.     (when (gnus-virtual-group-p group)
  430.       (let ((result (nnvirtual-find-group-art
  431.              (gnus-group-real-name group) article)))
  432.     (setq group (car result)
  433.           number (cdr result))))
  434.     (setq file (gnus-cache-file-name group number))
  435.     (when (and (file-exists-p file)
  436.            (or force
  437.            (gnus-cache-member-of-class
  438.             gnus-cache-remove-articles ticked dormant unread)))
  439.       (save-excursion
  440.     (delete-file file)
  441.     (set-buffer (cdr gnus-cache-buffer))
  442.     (goto-char (point-min))
  443.     (when (or (looking-at (concat (int-to-string number) "\t"))
  444.           (search-forward (concat "\n" (int-to-string number) "\t")
  445.                   (point-max) t))
  446.       (delete-region (progn (beginning-of-line) (point))
  447.              (progn (forward-line 1) (point)))))
  448.       (setq gnus-newsgroup-cached
  449.         (delq article gnus-newsgroup-cached))
  450.       (gnus-summary-update-secondary-mark article)
  451.       t)))
  452.  
  453. (defun gnus-cache-articles-in-group (group)
  454.   "Return a sorted list of cached articles in GROUP."
  455.   (let ((dir (file-name-directory (gnus-cache-file-name group 1)))
  456.     articles)
  457.     (when (file-exists-p dir)
  458.       (setq articles
  459.         (sort (mapcar (lambda (name) (string-to-int name))
  460.               (directory-files dir nil "^[0-9]+$" t))
  461.           '<))
  462.       ;; Update the cache active file, just to synch more.
  463.       (when articles
  464.     (gnus-cache-update-active group (car articles) t)
  465.     (gnus-cache-update-active group (car (last articles))))
  466.       articles)))
  467.  
  468. (defun gnus-cache-braid-nov (group cached &optional file)
  469.   (let ((cache-buf (get-buffer-create " *gnus-cache*"))
  470.     beg end)
  471.     (gnus-cache-save-buffers)
  472.     (save-excursion
  473.       (set-buffer cache-buf)
  474.       (buffer-disable-undo (current-buffer))
  475.       (erase-buffer)
  476.       (insert-file-contents (or file (gnus-cache-file-name group ".overview")))
  477.       (goto-char (point-min))
  478.       (insert "\n")
  479.       (goto-char (point-min)))
  480.     (set-buffer nntp-server-buffer)
  481.     (goto-char (point-min))
  482.     (while cached
  483.       (while (and (not (eobp))
  484.           (< (read (current-buffer)) (car cached)))
  485.     (forward-line 1))
  486.       (beginning-of-line)
  487.       (save-excursion
  488.     (set-buffer cache-buf)
  489.     (if (search-forward (concat "\n" (int-to-string (car cached)) "\t")
  490.                 nil t)
  491.         (setq beg (progn (beginning-of-line) (point))
  492.           end (progn (end-of-line) (point)))
  493.       (setq beg nil)))
  494.       (when beg
  495.     (insert-buffer-substring cache-buf beg end)
  496.     (insert "\n"))
  497.       (setq cached (cdr cached)))
  498.     (kill-buffer cache-buf)))
  499.  
  500. (defun gnus-cache-braid-heads (group cached)
  501.   (let ((cache-buf (get-buffer-create " *gnus-cache*")))
  502.     (save-excursion
  503.       (set-buffer cache-buf)
  504.       (buffer-disable-undo (current-buffer))
  505.       (erase-buffer))
  506.     (set-buffer nntp-server-buffer)
  507.     (goto-char (point-min))
  508.     (while cached
  509.       (while (and (not (eobp))
  510.           (looking-at "2.. +\\([0-9]+\\) ")
  511.           (< (progn (goto-char (match-beginning 1))
  512.                 (read (current-buffer)))
  513.              (car cached)))
  514.     (search-forward "\n.\n" nil 'move))
  515.       (beginning-of-line)
  516.       (save-excursion
  517.     (set-buffer cache-buf)
  518.     (erase-buffer)
  519.     (insert-file-contents (gnus-cache-file-name group (car cached)))
  520.     (goto-char (point-min))
  521.     (insert "220 ")
  522.     (princ (car cached) (current-buffer))
  523.     (insert " Article retrieved.\n")
  524.     (search-forward "\n\n" nil 'move)
  525.     (delete-region (point) (point-max))
  526.     (forward-char -1)
  527.     (insert "."))
  528.       (insert-buffer-substring cache-buf)
  529.       (setq cached (cdr cached)))
  530.     (kill-buffer cache-buf)))
  531.  
  532. ;;;###autoload
  533. (defun gnus-jog-cache ()
  534.   "Go through all groups and put the articles into the cache.
  535.  
  536. Usage:
  537. $ emacs -batch -l ~/.emacs -l gnus -f gnus-jog-cache"
  538.   (interactive)
  539.   (let ((gnus-mark-article-hook nil)
  540.     (gnus-expert-user t)
  541.     (nnmail-spool-file nil)
  542.     (gnus-use-dribble-file nil)
  543.     (gnus-novice-user nil)
  544.     (gnus-large-newsgroup nil))
  545.     ;; Start Gnus.
  546.     (gnus)
  547.     ;; Go through all groups...
  548.     (gnus-group-mark-buffer)
  549.     (gnus-group-iterate nil
  550.       (lambda (group)
  551.     (let (gnus-auto-select-next)
  552.       (gnus-summary-read-group group nil t)
  553.       ;; ... and enter the articles into the cache.
  554.       (when (eq major-mode 'gnus-summary-mode)
  555.         (gnus-uu-mark-buffer)
  556.         (gnus-cache-enter-article)
  557.         (kill-buffer (current-buffer))))))))
  558.  
  559. (defun gnus-cache-read-active (&optional force)
  560.   "Read the cache active file."
  561.   (gnus-make-directory gnus-cache-directory)
  562.   (if (or (not (file-exists-p gnus-cache-active-file))
  563.       force)
  564.       ;; There is no active file, so we generate one.
  565.       (gnus-cache-generate-active)
  566.     ;; We simply read the active file.
  567.     (save-excursion
  568.       (gnus-set-work-buffer)
  569.       (insert-file-contents gnus-cache-active-file)
  570.       (gnus-active-to-gnus-format
  571.        nil (setq gnus-cache-active-hashtb
  572.          (gnus-make-hashtable
  573.           (count-lines (point-min) (point-max)))))
  574.       (setq gnus-cache-active-altered nil))))
  575.  
  576. (defun gnus-cache-write-active (&optional force)
  577.   "Write the active hashtb to the active file."
  578.   (when (or force
  579.         (and gnus-cache-active-hashtb
  580.          gnus-cache-active-altered))
  581.     (nnheader-temp-write gnus-cache-active-file
  582.       (mapatoms
  583.        (lambda (sym)
  584.      (when (and sym (boundp sym))
  585.        (insert (format "%s %d %d y\n"
  586.                (symbol-name sym) (cdr (symbol-value sym))
  587.                (car (symbol-value sym))))))
  588.        gnus-cache-active-hashtb))
  589.     ;; Mark the active hashtb as unaltered.
  590.     (setq gnus-cache-active-altered nil)))
  591.  
  592. (defun gnus-cache-update-active (group number &optional low)
  593.   "Update the upper bound of the active info of GROUP to NUMBER.
  594. If LOW, update the lower bound instead."
  595.   (let ((active (gnus-gethash group gnus-cache-active-hashtb)))
  596.     (if (null active)
  597.     ;; We just create a new active entry for this group.
  598.     (gnus-sethash group (cons number number) gnus-cache-active-hashtb)
  599.       ;; Update the lower or upper bound.
  600.       (if low
  601.       (setcar active number)
  602.     (setcdr active number)))
  603.     ;; Mark the active hashtb as altered.
  604.     (setq gnus-cache-active-altered t)))
  605.  
  606. ;;;###autoload
  607. (defun gnus-cache-generate-active (&optional directory)
  608.   "Generate the cache active file."
  609.   (interactive)
  610.   (let* ((top (null directory))
  611.      (directory (expand-file-name (or directory gnus-cache-directory)))
  612.      (files (directory-files directory 'full))
  613.      (group
  614.       (if top
  615.           ""
  616.         (string-match
  617.          (concat "^" (file-name-as-directory
  618.               (expand-file-name gnus-cache-directory)))
  619.          (directory-file-name directory))
  620.         (nnheader-replace-chars-in-string
  621.          (substring (directory-file-name directory) (match-end 0))
  622.          ?/ ?.)))
  623.      nums alphs)
  624.     (when top
  625.       (gnus-message 5 "Generating the cache active file...")
  626.       (setq gnus-cache-active-hashtb (gnus-make-hashtable 123)))
  627.     ;; Separate articles from all other files and directories.
  628.     (while files
  629.       (if (string-match "^[0-9]+$" (file-name-nondirectory (car files)))
  630.       (push (string-to-int (file-name-nondirectory (pop files))) nums)
  631.     (push (pop files) alphs)))
  632.     ;; If we have nums, then this is probably a valid group.
  633.     (when (setq nums (sort nums '<))
  634.       (gnus-sethash group (cons (car nums) (gnus-last-element nums))
  635.             gnus-cache-active-hashtb))
  636.     ;; Go through all the other files.
  637.     (while alphs
  638.       (when (and (file-directory-p (car alphs))
  639.          (not (string-match "^\\.\\.?$"
  640.                     (file-name-nondirectory (car alphs)))))
  641.     ;; We descend directories.
  642.     (gnus-cache-generate-active (car alphs)))
  643.       (setq alphs (cdr alphs)))
  644.     ;; Write the new active file.
  645.     (when top
  646.       (gnus-cache-write-active t)
  647.       (gnus-message 5 "Generating the cache active file...done"))))
  648.  
  649. ;;;###autoload
  650. (defun gnus-cache-generate-nov-databases (dir)
  651.   "Generate NOV files recursively starting in DIR."
  652.   (interactive (list gnus-cache-directory))
  653.   (gnus-cache-close)
  654.   (let ((nnml-generate-active-function 'identity))
  655.     (nnml-generate-nov-databases-1 dir)))
  656.  
  657. (defun gnus-cache-move-cache (dir)
  658.   "Move the cache tree to somewhere else."
  659.   (interactive "FMove the cache tree to: ")
  660.   (rename-file gnus-cache-directory dir))
  661.  
  662. (provide 'gnus-cache)
  663.  
  664. ;;; gnus-cache.el ends here
  665.